package com.loong.member.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.member.common.forehead.param.CartForeheadParam;
import com.loong.member.model.Cart;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description:购物车持久层
 * @Author: 刘群
 * @Date: 2020/10/23 17:17
 */
@Repository
public class CartBusiness extends AbstractBusiness<Cart> {
    /**
     * 查询购物车
     *
     * @param param 查询条件为：用户id、商家id、购物车状态
     * @return 购物车集合
     */
    public List<Cart> findCartByParam(CartForeheadParam param) {
        Condition condition = new Condition(Cart.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、商家id、购物车状态为未付款(true)查询购物车
        criteria.andEqualTo("userId", param.getUserId());
        criteria.andEqualTo("sellerId", param.getSellerId());
        criteria.andEqualTo("cartStatus", true);
        return findByCondition(condition);
    }

    /**
     * 根据条件删除购物车
     *
     * @param userId 用户id
     * @param cartId 购物车id
     */
    public void deleteCartByCondition(Long userId, Long cartId) {
        Condition condition = new Condition(Cart.class);
        Example.Criteria criteria = condition.createCriteria();
        //根据用户id、购物车id删除购物车
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("id", cartId);
        this.deleteByCondition(condition);
    }
}
